package com.ygqh.baby.service.impl;

import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.constant.RedisConstant;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.AppMessageService;
import com.ygqh.baby.utils.UmPushApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AppMessageServiceImpl implements AppMessageService {
	@Autowired
	private RedisDao redisDao;

	private final static AppType[] appArr = { AppType.Android, AppType.Ios };

	@Override
	public void sendOrderDeliveryNotice(String orderCode, String userId, String orderType, String ticker, String title, String text, String description) {
		for (AppType appType : appArr) {
			String appToken = this.getAppToken(appType, userId);
			if (StringUtil.isBlank(appToken)) {
				continue;
			}
			Map<String, String> map = new HashMap<>();
			map.put("orderId", orderCode);
			map.put("orderType", orderType);
			map.put("key", "order");
			if (appType.equals(AppType.Android)) {

				UmPushApi.sendAndroidAotificationByAlias(appToken, "token", ticker, title, text, description, map);
			} else if (appType.equals(AppType.Ios)) {
				UmPushApi.sendIOSByAlias(appToken, "token", 1, null, title, text, map);
			}
		}

	}

	private String getAppToken(AppType appType, String userId) {
		return redisDao.get(String.format(RedisConstant.APP_TOKEN_KEY_PREFIX, appType.name(), userId.toString()));
	}

	@Override
	public void sendOrderDeliveryNotice(String orderCode, String userId, String orderType, String description) {

		this.sendOrderDeliveryNotice(orderCode, userId, orderType, "订单发货通知", "订单发货通知", "亲，您购买的壹果商品已经发货啦！收到商品后请给予评价哦！", description);
	}

}
